package com.iweb.user.service;

import com.iweb.user.dao.UserDao;
import com.iweb.user.dao.UserDaoImpl;
import com.iweb.user.model.User;
import javax.servlet.http.HttpSession;

public class UserServiceImpl implements UserService {
    // 创建UserDao实例用于数据库操作
    UserDao userDao = new UserDaoImpl();

    @Override
    public int saveOne(User user) {
        return userDao.saveOne(user);
    }

    @Override
    public boolean isLogin(String username, String password, HttpSession session) {
        // 根据用户名和密码查询用户
        User user = userDao.queryByUsernameAndPassword(username, password);

        // 如果查询到用户，保存用户信息到session并返回true
        if (user != null) {
            session.setAttribute("user", user);
            return true;
        }

        // 否则返回false
        return false;
    }

    @Override
    public boolean isContainUserName(String username) {
        return userDao.findByUsername(username) == 0;
    }

    @Override
    public User getUserById(int userId) {
        return userDao.getUserById(userId);
    }

    @Override
    public boolean deleteUser(int userId) {
        return userDao.deleteUser(userId);
    }

    @Override
    public boolean updateUser(User user) {
        return userDao.updateUser(user);
    }
}
